﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;

namespace DBKit5
{
    /// <summary>
    /// 主界面
    /// </summary>
    /// <remarks>2014-1-21 by sutroon</remarks>
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            // 设置界面布局
            //AppDataHelper.APP_DEBUG = true;

            ucConnectionBar1.ConnectionChangedHandler += ConnectionChangedEvent;
            ucConnectionBar1.RunSqlHandler += RunSql;
            ucTableObjects1.AfterSelectHandler += AfterSelectEvent;
            ucTableObjects1.ShowSqlHandler += ShowSqlEvent;
            ucCodeEditor1.RunScriptHandler += RunSql;
            ucCodeEditor1.RunForHandler += RunForEvent;

            MenuItemClickEvent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            ucConnectionBar1.uc_init(true);
            ucCodeEditor1.uc_init();
        }

        // ucConnectionBar1数据连接下拉框选项改变的处理方法
        private void ConnectionChangedEvent(IDatabaseHelper idh)
        {
            ucTableObjects1.DBH = idh;
            ucCodeEditor1.DBH = idh;
            ucQueryResult1.DBH = idh;
            ucTableObjects1.LoadTables();
        }

        // ucTableObjects1的树形列表选中对象改变的处理方法
        private void AfterSelectEvent(string objName, string objType)
        {
            ucCodeEditor1.ObjectName = objName;
            ucCodeEditor1.ObjectType = objType;
        }

        private void ShowSqlEvent(string sql,bool isExec)
        {
            ucCodeEditor1.SetText(sql, isExec);
        }

        #region 菜单点击事件配置
        
        private void MenuItemClickEvent()
        {
            msiExcel数据文件ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msiXML数据文件ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi安装目录ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi备份ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi编辑XML模型ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi导出ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi导入ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi多数据库作业ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi更新架构ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi关于ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi还原ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi加密解密ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi检查更新ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi生成测试数据ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi生成模板文件ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi树形层级助手ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi数据库连接ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
            msi通用权限助手ToolStripMenuItem.Click+=new EventHandler(MenuItem_Click);
            msi退出ToolStripMenuItem.Click+=new EventHandler(MenuItem_Click);
            msiUDM模型ToolStripMenuItem.Click += new EventHandler(MenuItem_Click);
        }

        private void MenuItem_Click(object sender, EventArgs e)
        {
            switch (((ToolStripMenuItem)sender).Name)
            {
                case "msiExcel数据文件ToolStripMenuItem":
                    new frmExcel2XmlConverter().Show();
                    break;
                case "msiXML数据文件ToolStripMenuItem":
                    new frmExcel2XmlConverter().Show();
                    break;
                case "msi安装目录ToolStripMenuItem":
                    System.Diagnostics.Process.Start(Application.StartupPath);
                    break;
                case "msi备份ToolStripMenuItem":
                    new frmDbBackup().Show();
                    break;
                case "msi编辑XML模型ToolStripMenuItem":
                    new frmModuleList().Show();
                    break;
                case "msiUDM模型ToolStripMenuItem":
                    new frmUDMToDatabase().Show();
                    break;
                case "msi导出ToolStripMenuItem":
                    break;
                case "msi导入ToolStripMenuItem":
                    break;
                case "msi多数据库作业ToolStripMenuItem":
                    new frmScriptDistribute().Show();
                    break;
                case "msi更新架构ToolStripMenuItem":
                    new frmDbUpdate().Show();
                    break;
                case "msi关于ToolStripMenuItem":
                    MessageBox.Show(AppDataHelper.GetAboutText(), "关于DBKit5");
                    break;
                case "msi还原ToolStripMenuItem":
                    new frmDbRestore().Show();
                    break;
                case "msi加密解密ToolStripMenuItem":
                    new frmEncrypt().Show();
                    break;
                case "msi检查更新ToolStripMenuItem":
                    break;
                case "msi生成测试数据ToolStripMenuItem":
                    break;
                case "msi生成模板文件ToolStripMenuItem":
                    new frmGenerateVarTemplate().Show();
                    break;
                case "msi树形层级助手ToolStripMenuItem":
                    break;
                case "msi数据库连接ToolStripMenuItem":
                    new frmConnectionList().Show();
                    break;
                case "msi通用权限助手ToolStripMenuItem":
                    break;
                case "msi退出ToolStripMenuItem":
                    Application.Exit();
                    break;
            }
        }

        #endregion

        // 执行SQL的处理方法
        private void RunSql(string sql)
        {
            if (string.IsNullOrEmpty(sql)) sql = ucCodeEditor1.GetSelectText();
            if (string.IsNullOrEmpty(sql)) return;
            string[] sqls = sql.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            ucQueryResult1.ExecuteSqls(sqls); ucCodeEditor1.ScriptHistorySet.Add(sql);
        }
        private void RunForEvent(string sql)
        {
            if (string.IsNullOrEmpty(sql)) return;
            new UControls.frmConnectionSelector() {SQLText=sql }.Show();
        }
    }
}
